package model.forview;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import controllers.routes;

import model.wrap.node.Node;
import model.wrap.structure.Structure;
import model.wrap.type.PointElement;
import models.ConstantName;
import models.ConstantValue;

/**
 * 
 * @author pocteg
 * 
 */
public class ContentChangePointModelForView extends ContentForMainTemplate
{
    private PointElement content;
    private ArrayList<Node> parents;
    private ArrayList<Node> children;
    private Node point;
    private ArrayList<Structure> allStructures;
    private Structure selectedStructure;

    public Long id_content;
    public Long id_structure;
    public Long id_point;

    public ContentChangePointModelForView(Map<String, String> form)
    {
	Iterator<String> keys = form.keySet().iterator();
	while (keys.hasNext())
	{
	    String key = keys.next();
	    if (key.equals("data[id_content]")) this.id_content = Long.valueOf(form.get(key));
	    if (key.equals("data[id_structure]")) this.id_structure = Long.valueOf(form.get(key));
	    if (key.equals("data[id_point]")) this.id_point = Long.valueOf(form.get(key));
	}
    }

    public ContentChangePointModelForView(Long idContent, Long idStructure, Long idPoint)
    {
	content = new PointElement(idContent);
	if (idStructure.equals(Long.valueOf(0)))
	{
	    point = content.getNode();
	    selectedStructure = content.getStructure();
	    children = point.getChildren();
	    parents = point.getParents();
	}
	else
	{
	    selectedStructure = new Structure(idStructure);
	    if (idPoint.equals(Long.valueOf(0)))
	    {
		point = null;
		parents = Node.getRootNodes(selectedStructure);
		children = new ArrayList<Node>();
	    }
	    else
	    {
		point = new Node(idPoint);
		children = point.getChildren();
		parents = point.getParents();
	    }
	}

	allStructures = Structure.getAllStructures();
	setDefaultValues();
    }

    /**
     * @author pocteg
     */
    private void setDefaultValues()
    {
	String title = ConstantValue.getConstantValue(ConstantName.CONTENT_CHANGE_POINT_PAGE_TITLE);
	String header = ConstantValue.getConstantValue(ConstantName.CONTENT_CHANGE_POINT_PAGE_HEADER);
	String footer = ConstantValue.getConstantValue(ConstantName.CONTENT_CHANGE_POINT_PAGE_FOOTER);
	String userName = "";

	title = title != null ? title : "CONTENT CHANGE TITLE";
	header = header != null ? header : "CONTENT CHANGE HEADER";
	footer = footer != null ? footer : "CONTENT CHANGE FOOTER";

	mainModelForView = new MainModelForView(title, userName, header, footer);
    }

    public long getIdContent()
    {
	return this.content.getId();
    }
    
    public String getContentTitleNode()
    {
	Node node = this.content.getContentNode();
	if(null != node) return node.getTitle();
	return "";	
    }
    
    public int getSizeOfAllStructures()
    {
	return this.allStructures.size();
    }

    public long getSelectedStructureId(int index)
    {
	return this.allStructures.get(index).getId();
    }

    public String getSelectedStructureTitle(int index)
    {
	return this.allStructures.get(index).getTitle();
    }

    public String getSelectedStructureIdSelect(int index)
    {
	if (this.allStructures.get(index).getId() == this.selectedStructure.getId()) return "selected";
	return "";
    }

    public int getSizeOfParents()
    {
	return this.parents.size();
    }

    public String getParentsTitle(int index)
    {
	return this.parents.get(index).getTitle();
    }

    public String getParentsUrl(int index)
    {
	String url;
	url = controllers.routes.Content.getPageContentChangePoint(this.content.getId(), this.parents.get(index).getStructure().getId(), this.parents.get(index).getId())
		.url();
	return url;
    }

    public int getSizeOfChildren()
    {
	return this.children.size();
    }

    public String getchildrenTitle(int index)
    {
	return this.children.get(index).getTitle();
    }

    public String getChildrenUrl(int index)
    {
	String url;
	url = controllers.routes.Content.getPageContentChangePoint(this.content.getId(), this.children.get(index).getStructure().getId(),
		this.children.get(index).getId()).url();
	return url;
    }

    public String showPoint()
    {
	if (this.point == null) return "none";
	return "";
    }

    public String getPointTitle()
    {
	if (this.point != null) return this.point.getTitle();
	return "";

    }

    public String getPointTypeTitle()
    {
	if (this.point != null) return this.point.getType().getTitle();
	return "";
    }

    public Long getFormContentId()
    {
	return this.content.getId();
    }

    public Long getFormStructureID()
    {
	if (this.point != null) return this.point.getStructure().getId();
	return new Long(0);
    }

    public Long getFormPointId()
    {

	if (this.point != null) return this.point.getId();
	return new Long(0);
    }
}
